package com.voicechat.firstmet.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.View;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseFragment;
import com.voicechat.firstmet.view.LazyViewPager;
import com.voicechat.firstmet.view.SimpleViewPagerIndicator;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 隐约
 * Created by Administrator on 2018/8/28.
 */
public class FaintFragment extends BaseFragment {

    @BindView(R.id.invest_indicator)
    SimpleViewPagerIndicator investIndicator;
    @BindView(R.id.invest_list)
    LazyViewPager investList;
    Unbinder unbinder;
    private String[] mTitles;
    private ArrayList<Fragment> fragments;
    private MyAdapter mAdapter;
    private String TAG = "FaintFragment";

    @Override
    protected int loadLayout() {
        return R.layout.fragment_faint_list;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this, view);
        initView();
    }

    private void initView() {
        mTitles = getResources().getStringArray(R.array.faint_list);
        initIndicator();
        bindEvent();
        initViewPager();
    }
    private void initViewPager() {
        fragments = new ArrayList<>();
        fragments.add(ShareGirlsFragment.newInstance());
        fragments.add(SharePictureFragment.newInstance());
        fragments.add(AttachmentPersonFragment.newInstance());
        /*fragments.add(VoiceChatListFragment.newInstance("CITY"));*/
        mAdapter = new MyAdapter(getChildFragmentManager());
        investList.setAdapter(mAdapter);
    }
    /**
    * @method
    * @description 设置当前显示fragment
    * @date: 2019/1/30 10:02
    * @author: ljp
    * @return
    */
    public void setFragments(int flag){
        if(investIndicator != null){
            if(investList.getCurrentItem() != flag){
                investIndicator.clearFocus();
               // investIndicator.setCurrentItem(investList.getCurrentItem());
                investIndicator.setCurrentItem(flag);
                investList.setCurrentItem(flag);
            }
        }
    }
    private void bindEvent() {
        investIndicator.setOnPageChangeListener(new SimpleViewPagerIndicator.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected: ----------------------"+position);
                investIndicator.setCurrentItem(position);
            }
        });
    }
    private void initIndicator() {
        if (null == mTitles || mTitles.length == 0) {
            investIndicator.setVisibility(View.GONE);
        } else {
            investIndicator.setTitles(mTitles);
            investIndicator.setViewPager(investList);
            investList.setCurrentItem(0);
        }
    }
    private class MyAdapter extends FragmentStatePagerAdapter {

        public MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }
        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return fragments.get(arg0);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return fragments.size();
        }

    }
}
